package com.shenma2009.controller;

import com.shenma2009.vo.KafkaMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.Collections;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

/**
 * @author 军哥
 * @version 1.0
 * @description: KafkaController
 * @date 2023/7/31 15:20
 */

@RestController
@Slf4j
@RequestMapping("/kafka")
public class KafkaController {

    @Autowired
    KafkaTemplate<String, Object> kafkaTemplate;

    @PostMapping(value = "/send01")
    public String send01(@RequestBody KafkaMessage kafkaMessage) {
        kafkaTemplate.send("topic-2102", kafkaMessage.getMesssage());
        System.out.println("SEND:"+kafkaMessage.getMesssage());

        return kafkaMessage.getMesssage();
    }

    /**
     * @description Clients 发送消息
     * @author 军哥
     * @date 2023/8/2 9:20
     * @version 1.0
     */
    @PostMapping(value = "/send02")
    public String send02(@RequestBody KafkaMessage kafkaMessage) {
        // 配置Kafka生产者的属性
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");  // Kafka broker的地址和端口
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        // 创建Kafka生产者实例
        Producer<String, String> producer = new KafkaProducer<>(props);

        try {
            // 发送消息到指定topic
            String topic = "test-topic";
            String key = "message-key";
            String value = kafkaMessage.getMesssage();
            ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, value);

            producer.send(record);
            System.out.println("SEND："+kafkaMessage.getMesssage());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.close();  // 关闭Kafka生产者实例
        }

        return kafkaMessage.getMesssage();
    }

    /**
     * @description Clients 收消息
     * @author 军哥
     * @date 2023/8/2 9:22
     * @version 1.0
     */
    @PostMapping(value = "/recv02")
    public String recv02() {
        // 创建消费者配置
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "your_consumer_group_id");

        // 创建消费者实例
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

        // 订阅主题
        consumer.subscribe(Collections.singletonList("test-topic"));

        // 消费消息
        try {
            do {
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
                for (ConsumerRecord<String, String> record : records) {
                    System.out.println("接收到消息：key = " + record.key() + ", value = " + record.value());
                    // 处理接收到的消息
                }
            } while (false);
        } finally {
            consumer.close();
        }

        return "OK";
    }

}
